package game.gui.panelconversation;

import game.controler.ControlerPanelConversation;
import game.controler.ecouteurs.conversation.EcouteurConversationReponse;
import game.controler.gestionnaires.conversation.GestionnaireReponses;
import game.controler.gestionnaires.elementjeu.GestionnaireFous;
import game.controler.gestionnaires.elementjeu.GestionnairePnj;
import game.model.conversation.Dialogue;
import game.model.conversation.Reponse;
import game.model.objets.Fou;
import game.model.objets.PNJ;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 *
 * @author Gab'z
 */
public class PanelConvDialogue extends JPanel {

    private static PanelConvDialogue panelConvDialogue = null;
    private static JPanel medaillon = new JPanel();
    private static JPanel zoneTexte = new JPanel();
    private static JPanel panelZoneTexte = new JPanel(new BorderLayout());
    private static JTextArea jTextArea;

    private PanelConvDialogue() {
    }

    public static PanelConvDialogue getPanelConvDialogue() {
        if (panelConvDialogue == null) {
            panelConvDialogue = new PanelConvDialogue();
        }
        medaillon.setLocation(0, 0);
        medaillon.setSize(120, 120);
        medaillon.setMaximumSize(new Dimension(120, 120));
        medaillon.setPreferredSize(new Dimension(150, 120));
        medaillon.setMinimumSize(new Dimension(120, 120));
        medaillon.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        zoneTexte.setLocation(130, 0);
        zoneTexte.setSize(480, 250);
        zoneTexte.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        panelZoneTexte.setPreferredSize(new Dimension(480, 100));

        return panelConvDialogue;
    }

    public void setDialogue(Dialogue dialogue, String typeInterlocuteur) {
        this.removeAll();

        System.out.println("########### setDialogue");
        medaillon.removeAll();
        medaillon.setLayout(new BoxLayout(medaillon, BoxLayout.Y_AXIS));
        System.out.println("typeInterlocuteur = " + typeInterlocuteur);
        System.out.println("id interlocuteur = " + dialogue.getIdPersonnage());
        JLabel labelPortrait = new JLabel();
        if ("Fou".equals(typeInterlocuteur)) {
            labelPortrait = new JLabel(GestionnaireFous.getGfous().getFou(dialogue.getIdPersonnage()).getImgInventaire());
        } else {
            labelPortrait = new JLabel(GestionnairePnj.getGpnj().getPNJ(dialogue.getIdPersonnage()).getImgPortrait());
        }
        //  labelPortrait.setLocation(35, 20);
        labelPortrait.setMaximumSize(new Dimension(80, 80));
        labelPortrait.setPreferredSize(new Dimension(80, 80));
        labelPortrait.setMinimumSize(new Dimension(80, 80));
        labelPortrait.setAlignmentX(CENTER_ALIGNMENT);
        medaillon.add(labelPortrait);
        JLabel labelPrenomNom = new JLabel();
        if ("Fou".equals(typeInterlocuteur)) {
            Fou fou = GestionnaireFous.getGfous().getFou(dialogue.getIdPersonnage());
            labelPrenomNom = new JLabel(fou.getPrenom() + fou.getNom());
        } else {
            PNJ pnj = GestionnairePnj.getGpnj().getPNJ(dialogue.getIdPersonnage());
            labelPrenomNom = new JLabel(pnj.getPrenom() + pnj.getNom());
        }
        // labelPrenomNom.setLocation(10, 150);
        //labelPrenomNom.setSize(120, 40);
        labelPrenomNom.setAlignmentX(CENTER_ALIGNMENT);

        medaillon.add(labelPrenomNom);
        medaillon.validate();
        medaillon.repaint();
        this.add(medaillon);




        zoneTexte.removeAll();
        zoneTexte.setLayout(new BoxLayout(zoneTexte, BoxLayout.Y_AXIS));

        panelZoneTexte.removeAll();

        String text = "";
        for (String phrase : dialogue.getListePhrases()) {
            System.out.println("PHRASE : " + phrase);
            text += phrase + "\n\n";
        }
        jTextArea = new JTextArea(text);
        jTextArea.setLineWrap(true);
        jTextArea.setEditable(false);
        panelZoneTexte.add(jTextArea, BorderLayout.CENTER);
        // panelZoneTexte.add(labelSuivant, BorderLayout.SOUTH);
        panelZoneTexte.validate();
        panelZoneTexte.repaint();
        panelZoneTexte.setAlignmentX(LEFT_ALIGNMENT);

        zoneTexte.add(panelZoneTexte);
        JTextArea textReponse = new JTextArea();
        //  JLabel labelReponse = new JLabel();
        System.out.println("DIALOGUE.reponse = " + dialogue.getIdReponses().size());
        for (Reponse reponse : GestionnaireReponses.getGReponse().getReponsesForDialogue(dialogue.getIdReponses())) {

            System.out.println("REPONSE = " + reponse.getPhrase());
            textReponse = new JTextArea(" - " + reponse.getPhrase());
            textReponse.setEditable(false);
            textReponse.setLineWrap(true);
            textReponse.setBackground(Color.LIGHT_GRAY);
            textReponse.setAlignmentX(LEFT_ALIGNMENT);
            textReponse.addMouseListener(new EcouteurConversationReponse(reponse.getId()));
            zoneTexte.add(textReponse);
//                labelReponse = new JLabel(reponse.getPhrase());
//                labelReponse.setAlignmentX(LEFT_ALIGNMENT);
//                labelReponse.addMouseListener(new EcouteurConversationReponse(reponse.getId()));
//                zoneTexte.add(labelReponse);
//            
        }

        //labelSuivant.setSize(40, 30);
        //labelSuivant.setLocation(200, 160);

        JLabel bufferLabel;
        if (!ControlerPanelConversation.getBuffer().isEmpty()) {

            bufferLabel = new JLabel(" ");
            bufferLabel.setAlignmentX(LEFT_ALIGNMENT);
            zoneTexte.add(bufferLabel);
        }

        for (String ligne : ControlerPanelConversation.getBuffer()) {
            bufferLabel = new JLabel(ligne);
            bufferLabel.setAlignmentX(LEFT_ALIGNMENT);
            zoneTexte.add(bufferLabel);
        }
        ControlerPanelConversation.resetBuffer();

        zoneTexte.validate();
        zoneTexte.repaint();
        this.add(zoneTexte);

        this.validate();
        this.repaint();
    }
}
